fb = console.log;

function $(id){
    return document.getElementById(id);
}//$
var intervalH = 0;
function init(){

    if (window.menu) {
        menu.hideSoftkeys();
    }
    
    var chooserEls = '';
    for (var i = 0; i < 60; i++) {
        chooserEls += '<input type="button" value="' + i + '">';
    }
    $('chooser').innerHTML += chooserEls;
    
    
    var clickedEl = '';
    $('min').onclick = $('sec').onclick = function(){
        $('chooser').style.display = 'block';
        clickedEl = this;
    }
    
    var chooserButtons = $('chooser').childNodes;
    
    for (var i = chooserButtons.length - 1; i >= 0; i--) {
        var b = chooserButtons[i];
        if (/input/i.test(b.tagName)) {
            b.onclick = function(e){
                clickedEl.value = e.currentTarget.value != 0 ? e.currentTarget.value : '00';
                $('chooser').style.display = 'none';
                $('start').disabled = false;
            }//onclick
        }//if
    }//for
    var predefinedButtons = $('predefinedButtons').childNodes;
    for (var i = predefinedButtons.length - 1; i >= 0; i--) {
        var b = predefinedButtons[i];
        if (/input/i.test(b.tagName)) {
            b.onclick = function(e){
				$('start').disabled = false;
                $('sec').value = '00';
                $('min').value = '00';
                var clickedButton = e.currentTarget;
                if (/\d+s$/.test(clickedButton.id)) {
                    $('sec').value = clickedButton.id.replace('s', '');
                }
                if (/\d+m$/.test(clickedButton.id)) {
                    $('min').value = clickedButton.id.replace('m', '');
                }
                if (parseInt($('min').value) != 0 && parseInt($('sec').value) != 0) {
                    $('start').disabled = false;
                }
            }//onclick
        }//if
    }//for
    $('start').disabled = true;
    $('stop').style.display = 'none';
    
    $('start').onclick = function(){
        $('predefinedButtons').style.display = 'none';
        $('min').disabled = true;
        $('sec').disabled = true;
        this.disabled = true;
        $('stop').style.display = 'inline';
        
        var t = 0;
        t += $('sec').value * 1000;
        t += $('min').value * 1000 * 1000;
        // setTimeout(play, t);
        $('musicContainer').innerHTML = '';
        startCoundown();
        
    }//onclick
    $('stop').onclick = function(){
        clearInterval(intervalH);
        $('predefinedButtons').style.display = 'block';
        $('min').disabled = false;
        $('sec').disabled = false;
        $('start').disabled = false;
        
    }//onclick
}//init
function startCoundown(){
    intervalH = setInterval(updateTime, 1000);
}//startCoundown
function updateTime(){
    var m = parseInt($('min').value);
    var s = parseInt($('sec').value);
    
    if (s >= 0) {
        s--;
        if (s < 0 && m >= 0) {
            m--;
            if (s < 0 && m < 0) {
				clearInterval(intervalH);
				$('predefinedButtons').style.display = 'block';
				$('stop').style.display = 'none';
				$('min').disabled = false;
				$('sec').disabled = false;
				$('start').disabled = false;
				
				play();
				
				return;
			} else {
				s = 59;
			}//if
        }//if
    }//if
    
    $('min').value = m;
    $('sec').value = s;
    
}//updateTime
function play(){
    var m = '<embed style="height:0" src="music/bgm00.mid" loop="false" autostart="true" hidden="true" id="music">';
    $('musicContainer').innerHTML = m;
}//play

